package com.spzx.cart.controller;

import com.spzx.cart.domain.CartInfo;
import com.spzx.cart.service.ICartService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {

    @Autowired
    private ICartService cartService;


    @Operation(summary = "添加购物车")
    @RequiresLogin
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
                                @Parameter(name = "skuNum", description = "数量", required = true) @PathVariable("skuNum") Integer skuNum) {
        cartService.addToCart(skuId, skuNum);
        return success();
    }

    @GetMapping("cartList")
    public AjaxResult cartList(){
        List<CartInfo> cartList = cartService.cartList();
        return AjaxResult.success(cartList) ;
    }

    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable Long skuId){
        cartService.deleteCart(skuId);
        return AjaxResult.success() ;
    }

    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable Long skuId,@PathVariable Integer isChecked ){
        cartService.checkCart(skuId,isChecked);
        return AjaxResult.success();
    }

    @GetMapping("allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable Integer isChecked){
        cartService.allCheckCart(isChecked);
        return AjaxResult.success();
    }

    @GetMapping("clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return AjaxResult.success();
    }


    @InnerAuth
    @GetMapping("/getCartCheckedList/{userId}")
    public R<List<CartInfo>> getCartCheckedList( @PathVariable Long userId) {
        return R.ok(cartService.getCartCheckedList(userId));

    }

    @InnerAuth
    @DeleteMapping("removeCheckedCart")
    public void removeCheckedCart(@RequestBody List<Long>skuIds) {
        cartService.removeCheckedCart(skuIds);
    }
}